// EPropertyFile.cpp: implementation of the EPropertyFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IPropertyHost.h"
#include "EPropertyFile.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

EPropertyFile::EPropertyFile(IPropertyHost *pHost, const std::wstring &sName, std::wstring *pProperty, const std::wstring &sFilters) : EPropertyString(pHost, sName, pProperty)
{
    m_sFilters = sFilters;
}

EPropertyFile::~EPropertyFile()
{}

bool EPropertyFile::HasButton()
{
    return true;
}

bool EPropertyFile::OnCommand(EPropWnd *pParent, WPARAM wParam, LPARAM lParam)
{
    bool bPropertyChanged = false;

    //	CFileDialog dia(TRUE,NULL,m_sProperty,OFN_HIDEREADONLY,m_sFilters,NULL);
    CFileDialog dia(TRUE, NULL, NULL, OFN_HIDEREADONLY, m_sFilters.c_str(), NULL);

    if(dia.DoModal() == IDOK) {
        std::wstring s = dia.GetPathName().GetString();

        if(m_pHost->PropertyChanging(m_pProperty, &s)) {
            *(std::wstring *)m_pProperty = s;
            bPropertyChanged = true;
        }
    }

    return bPropertyChanged;
}

//"Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";

void EPropertyFile::AddFilterItem(std::wstring sDescription, std::wstring sFilter)
{
    CStringW sNew;
    std::wstring sOld = m_sFilters;

    sNew.Format(L"%s|%s", sDescription.c_str(), sFilter.c_str());

    auto ret = CStringW();
    ret.Format(L"%s|%s", sNew, sOld.c_str());
    m_sFilters = ret.GetString();
}